#!/bin/env python
#-*- coding:utf-8 -*-

#----------------------------------------------------------------------------
# Name     :      GUI.py
# Purpose  :      Mysql tool for ucweb, and this is the py file of gui.
# Author   :      Albert cheng
# Created  :      2012-10-29
# Copyright:     (c) 2012 by Albertcheng
# Licence  :      wxWindows license
#----------------------------------------------------------------------------

import wx
from config_panel import config_panel
from tianjie_panel import tianjie_panel
from ssdz_panel import ssdz_panel
from request_handler import request_sqlite

class new_notebook(wx.Frame):
    """
    new designed notebook page.  新写的主页面样式，用了插页方式
    
    """
    
    def __init__(self, parent, id , log, title, Type):
        
        self.mainframe = wx.Frame.__init__(self, parent, -1, title, size = (1030, 700),
                          style= wx.CAPTION| wx.MINIMIZE_BOX |wx.CLOSE_BOX| wx.NO_FULL_REPAINT_ON_RESIZE)
        
        self.nb = wx.Notebook(self)
        self.log = log
        self.request_sqlite = request_sqlite(self.log)
        
        
        il = wx.ImageList(24, 24)
        
        
        self.il0 = il.Add(wx.Image(r'./icon/ssdz.png',wx.BITMAP_TYPE_ANY).Rescale(24,24).ConvertToBitmap())
        self.il1 = il.Add(wx.Image(r'./icon/tj.png',wx.BITMAP_TYPE_ANY).Rescale(24,24).ConvertToBitmap())
        self.il2 = il.Add(wx.Image(r'./icon/config.png',wx.BITMAP_TYPE_ANY).Rescale(24,24).ConvertToBitmap())
                    
        
        self.nb.AssignImageList(il)
        self.config_panel = config_panel(self.nb, self.log)
        page_def = [
                    [ssdz_panel(self.nb, self.log),       "松鼠大战".decode("utf-8")],
                    [tianjie_panel(self.nb, self.log),    "天劫".decode("utf-8")],
                    [self.config_panel,     "配置".decode("utf-8")],
                    ]
        
        for npage_idx in range(len(page_def)):
            self.nb.AddPage(page_def[npage_idx][0], page_def[npage_idx][1])
            self.nb.SetPageImage(npage_idx, getattr(self,"il"+str(npage_idx)))
            
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
        
        self.Bind(wx.EVT_CLOSE, self.Evt_OnCloseWindow)
        
        if Type == "config":
            self.nb.SetSelection(2)
        else:
            self.nb.SetSelection(self.request_sqlite.dosql(["select * from prefer;",])[0][1])
        
    def OnPageChanged(self, event):
        setattr(self.nb,"tianjie_refresh_mysql_flag",False)
        setattr(self.nb,"ssdz_refresh_mysql_flag",False)
        
        if int(self.nb.GetSelection()) != 2:# 2 --> config_panel
            #save mysql config data
            host_str = self.config_panel.host.GetValue()
            port_str = int(self.config_panel.port.GetValue())
            user_str = self.config_panel.user.GetValue()
            passwd_str = self.config_panel.passwd.GetValue()  
            charset_str = self.config_panel.charset.GetValue()
            
            sqls = [
                    "delete from config;",
                    "insert into config values(null,'1','%s','%s','%s','%s','%s');" % (host_str,port_str,user_str,passwd_str,charset_str),
                    ]
            
            self.request_sqlite.dosql(sqls)            

        event.Skip()   
             
    def Evt_OnCloseWindow(self, evt):
        setattr(self.nb,"tianjie_refresh_mysql_flag",False)
        setattr(self.nb,"ssdz_refresh_mysql_flag",False)
        try:
            #save mysql config data
            host_str = self.config_panel.host.GetValue()
            port_str = int(self.config_panel.port.GetValue())
            user_str = self.config_panel.user.GetValue()
            passwd_str = self.config_panel.passwd.GetValue()  
            charset_str = self.config_panel.charset.GetValue()
            
            sqls = [
                    "delete from config;",
                    "insert into config values(null,'1','%s','%s','%s','%s','%s');" % (host_str,port_str,user_str,passwd_str,charset_str),
                    ]
            if int(self.nb.GetSelection()) != 2:
                sqls += ["delete from prefer;",
                    "insert into prefer values(null,'%s');" % (self.nb.GetSelection()),
                    ]
        except:
            sqls = []
        
        self.request_sqlite.dosql(sqls)
        
        try:
            self.thread_abortEvent.set()
        except:
            self.Destroy()
            
        self.Destroy()
         
